package cn.ad.service;

import cn.ad.handler.SqlSessionHandler;
import cn.ad.interceptor.PageParameter;
import cn.ad.model.AccountModel;
import cn.ad.model.ResultSetList;
import cn.ad.model.TransferModel;
import cn.ad.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 资金流水
 * @author shirongkang
 *
 */
@Service
public class AccountService {

    @Autowired
    private SqlSessionHandler sqlSessionHandler;

    @Autowired
    private AdvertiserService advertiserService;

    @Autowired
    private AgencyService agencyService;

    @Transactional(readOnly = true)
    public ResultSetList<TransferModel> findAccountsByAngency(Map<String,Object> params, PageParameter pageInfo) {
        List<TransferModel> transferModelList = sqlSessionHandler.selectList("agencyMapper.agency:list:page",params, pageInfo);
        ResultSetList<TransferModel> list = new ResultSetList<>(pageInfo.getTotalCount(), transferModelList);
        return list;
    }

    @Transactional(readOnly = true)
    public ResultSetList<TransferModel> findAccountsByAdvertiser(Map<String,Object> params, PageParameter pageInfo) {
        List<TransferModel> transferModelList = sqlSessionHandler.selectList("advertiserMapper.advertiser:list:agencyId:page",params, pageInfo);
        ResultSetList<TransferModel> list = new ResultSetList<>(pageInfo.getTotalCount(), transferModelList);
        return list;
    }

    @Transactional(readOnly = true)
    public List<AdvertiserInfo> findAccountsByAdvertiser(Integer agencyId) {
        return sqlSessionHandler.selectList("advertiser:ByAgencyId:list",agencyId);
    }

    @Transactional(readOnly = true)
    public ResultSetList<AccountModel> findAccounts(Map<String,Object> params, PageParameter pageInfo) {
        List<AccountModel> accountModelList = sqlSessionHandler.selectList("accountTransferRecordsMapper.transfer:list:page",params, pageInfo);
        ResultSetList<AccountModel> list = new ResultSetList<>(pageInfo.getTotalCount(), accountModelList);
        return list;
    }

    @Transactional(readOnly = true)
    public TransferModel findAgencybyId(int id) {
        return sqlSessionHandler.selectOne("agencyMapper.selectOneById",id);
    }

    @Transactional(readOnly = true)
    public AdvertiserInfo findAdvertiserbyId(int id) {
        return sqlSessionHandler.selectOne("advertiserMapper.selectOne",id);
    }

    @Transactional()
    public void saveAccount(AccountTransferRecords accountTransferRecords) {
        Advertiser advertiser = null;Agency agency = null;
       switch (accountTransferRecords.getFromType()) {
           case 1:
               advertiser = new Advertiser();
               advertiser.setId(accountTransferRecords.getFromId());
               advertiser.setBalanceCash(accountTransferRecords.getAmount());
               advertiserService.updateAdvertiserAmount(advertiser,false);
               break;
           case 2:
               agency = new Agency();
               agency.setId(accountTransferRecords.getFromId());
               agency.setBalanceCash(accountTransferRecords.getAmount().doubleValue());
               agencyService.updateAgencyAmount(agency,false);
               break;
       }
       switch (accountTransferRecords.getToType()) {
           case 1:
               advertiser = new Advertiser();
               advertiser.setId(accountTransferRecords.getToId());
               advertiser.setBalanceCash(accountTransferRecords.getAmount());
               advertiserService.updateAdvertiserAmount(advertiser,true);
               break;
           case 2:
               agency = new Agency();
               agency.setId(accountTransferRecords.getToId());
               agency.setBalanceCash(accountTransferRecords.getAmount().doubleValue());
               agencyService.updateAgencyAmount(agency,true);
               break;
       }
        sqlSessionHandler.selectOne("accountTransferRecordsMapper.insert",accountTransferRecords);
    }

    @Transactional()
    public void saveCampaginAccount(AccountCampaignRecords accountCampaignRecords) {
        sqlSessionHandler.selectOne("accountCampaignRecordsMapper.insert",accountCampaignRecords);
    }
}
